import java.util.HashMap;
import java.util.Map;

public class num76 {
    Map<Character,Integer> map1 = new HashMap<>();
    Map<Character,Integer> map2 = new HashMap<>();
    public String minWindow(String s, String t) {
        for (int i = 0; i < t.length(); i++) {
            map1.put(t.charAt(i),map1.getOrDefault(t.charAt(i),0) + 1);
        }
        int l = 0,r = -1;
        int len = Integer.MAX_VALUE;
        int ansl = -1,ansr = -1;
        while (r < s.length()) {
            r ++;
            if(r < s.length() && map1.containsKey(s.charAt(r))) {
                map2.put(s.charAt(r),map2.getOrDefault(s.charAt(r),0) + 1);
            }
            while (check() && l <= r) {
                if(r - l + 1 < len) {
                    len = r - l + 1;
                    ansl = l;
                    ansr = l + len;
                }
                if(map1.containsKey(s.charAt(l))) {
                    map2.put(s.charAt(l),map2.getOrDefault(s.charAt(l),0) - 1);
                }
                l ++;
            }
        }
        return ansl == -1? "":s.substring(ansl,ansr);
    }
    public boolean check() {
        for(Map.Entry entry : map1.entrySet()) {
            int val = (Integer) entry.getValue();
            Character key = (Character) entry.getKey();
            if(map2.getOrDefault(key,0) < val) {
                return false;
            }
        }
        return  true;
    }
}
